package main

import "fmt"

// defer的调用是有个栈的
func main() {
	demo1(100)
	demo2([]int{1, 2, 3})
	demo3(User{ID: 1, Name: "xiaoming"})
	// u := User{ID: 1, Name: "xiaoming"}
	demo4(&User{ID: 1, Name: "xiaoming"})
}

// 嗯嗯 是值类型
func demo1(i int) {
	defer fmt.Println("i1", i)
	defer fmt.Println("i2", i)

	i++
	fmt.Println("i3", i)
}

// 嗯嗯 是值类型
func demo2(ii []int) {
	defer fmt.Printf("ii1: %#v\n", ii)
	defer fmt.Printf("ii2: %#v\n", ii)
	ii = append(ii, 3)
	fmt.Printf("ii3: %#v\n", ii)
}

// 嗯嗯 还是值类型
func demo3(s User) {
	defer fmt.Printf("s1: %#v\n", s)
	defer fmt.Printf("s2: %#v\n", s)
	s.ID++
	s.Name = "hehe"
	fmt.Printf("s3: %#v\n", s)
}

// 嗯嗯 这个就是引用类型了，good
func demo4(s *User) {
	defer fmt.Printf("s1: %#v\n", s)
	defer fmt.Printf("s2: %#v\n", s)
	s.ID++
	s.Name = "hehe"
	fmt.Printf("s3: %#v\n", s)
}

type User struct {
	ID   int
	Name string
}
